package com.futu.intelligence.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api/route")
public class RouteController {

    @Value("${amap.key}")
    private String amapKey;

    @Value("${amap.route-url}")
    private String routeUrl;

    @GetMapping("/driving")
    public ResponseEntity<?> getDrivingRoute(
            @RequestParam String origin,         // 起点 "经度,纬度"
            @RequestParam String destination,    // 终点 "经度,纬度"
            @RequestParam(required = false) String waypoints // 途经点 "经度1,纬度1|经度2,纬度2"
    ) {
        String url = routeUrl + "?key=" + amapKey
                + "&origin=" + origin
                + "&destination=" + destination
                + "&polyline=1";
        if (waypoints != null && !waypoints.isEmpty()) {
            url += "&waypoints=" + waypoints;
        }

        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);

        // 直接返回高德原始结果，前端可自行解析
        return ResponseEntity.ok(result);
    }

    @GetMapping("/walking")
    public ResponseEntity<?> getWalkingRoute(
            @RequestParam String origin,
            @RequestParam String destination
    ) {
        String url = "https://restapi.amap.com/v3/direction/walking?key=" + amapKey
                + "&origin=" + origin
                + "&destination=" + destination
                + "&polyline=1";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/transit")
    public ResponseEntity<?> getTransitRoute(
            @RequestParam String origin,
            @RequestParam String destination,
            @RequestParam String city
    ) {
        String url = "https://restapi.amap.com/v3/direction/transit/integrated?key=" + amapKey
                + "&origin=" + origin
                + "&destination=" + destination
                + "&city=" + city
                + "&polyline=1";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return ResponseEntity.ok(result);
    }
}
